 <template>
   <el-card shadow="always" class="box-card">
      <div class="card-name search-item">
        <div class="name width2">包装材料列表</div>
        <div class="mr-15">
          <el-button size="medium" type="primary" @click="add">新 增</el-button>
          <el-button size="medium" type="danger">刷 新</el-button>
        </div>
      </div>
      <div class="content-con">
        <el-form :model="form" ref="form" size="medium" label-width="100px">
          <el-row type="flex" justify="start">
            <el-form-item label="类别名称:" prop="pack_type">
              <el-input v-model="form.pack_type" clearable placeholder="输入类别名称查询" />
            </el-form-item>
            <el-button class="save mb-22 ml-20" @click="search">查 询</el-button>
          </el-row>
        </el-form>
        <div class="form-item">
          <el-table
            :data="tableData.slice((currentPage-1)*pagesize,currentPage*pagesize)"
            class="table"
            stripe border
            :header-cell-style="{background:'#f3f3f3',textAlign: 'center'}"
            :cell-style="{ textAlign: 'center' }">
            <el-table-column prop="pack_type" label="类别">
              <template slot-scope="scope">
                <span v-for="item in packList" :key="item.value">
                  <span v-if="scope.row.pack_type == item.value">{{item.label}}</span>
                </span>
              </template>
            </el-table-column>
            <el-table-column prop="model" label="规格">
              <template slot-scope="scope">
                <span v-for="item in modelList" :key="item.value">
                  <span v-if="scope.row.model == item.value">{{item.label}}</span>
                </span>
              </template>
            </el-table-column>
            <el-table-column prop="amount" label="库存数量">
            </el-table-column>
            <el-table-column prop="unit" label="单位">
            </el-table-column>
            <el-table-column prop="price" label="成本价(元)">
            </el-table-column>
            <el-table-column label="操作">
              <template slot-scope="scope">
                <el-button type="text" @click="handleEdit(scope.row)">
                  <i class="el-icon-edit"></i>编辑
                </el-button>
                <el-button type="text" @click.native.prevent="deleteRow(scope.$index,tableData)">
                  <i class="el-icon-delete"></i>删除
                </el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
        <!-- 分页-页码 -->
        <div class="page">
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="currentPage"
            :page-sizes="[5, 10, 15, 20]"
            :page-size="pagesize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="tableData.length">
          </el-pagination>
        </div>
      </div>
      <el-dialog
        :title="text"
        :visible.sync="dialogAdd"
        width="30%">
        <el-form :model="addForm" ref="addForm" size="medium" label-width="100px">
          <el-form-item label="包材类别:" prop="pack_type">
            <el-select v-model="addForm.pack_type" filterable clearable placeholder="包材类别" style="width: 100%;">
              <el-option
                v-for="item in packList"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="规格:" prop="model">
            <el-select v-model="addForm.model" filterable clearable placeholder="规格" style="width: 100%;">
              <el-option
                v-for="item in modelList"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="库存数量:" prop="amount">
            <el-input v-model="addForm.amount" placeholder="库存数量"></el-input>
          </el-form-item>
          <el-form-item label="单位:" prop="unit">
            <el-input v-model="addForm.unit" placeholder="请输入单位"></el-input>
          </el-form-item>
          <el-form-item label="成本价(元):" prop="price">
            <el-input v-model="addForm.price" placeholder="请输入成本价(元)"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button type="success" @click="save">保 存</el-button>
          <el-button @click="close">取 消</el-button>
        </span>
      </el-dialog>
   </el-card>

 </template>
<script>

export default {
  data () {
    return {
      dialogAdd: false,
      currentPage: 1, //当前页码
      pagesize: 5,    //每页的数据条数
      //tableData: [] //表格数据
      text:'',
      tableData: [
        {
          pack_type: '0',
          model: '0',
          amount: '500',
          unit: '个',
          price: '0.2'
        },
        {
          pack_type: '0',
          model: '1',
          amount: '500',
          unit: '个',
          price: '0.4'
        },
        {
          pack_type: '2',
          model: '0',
          amount: '1000',
          unit: '个',
          price: '0.1'
        },
        {
          pack_type: '2',
          model: '1',
          amount: '1000',
          unit: '个',
          price: '0.2'
        }
      ],
      form: {
        pack_type: ''
      },
      addForm: {
        pack_type: '',
        model: '',
        amount: '',
        unit: '',
        price: ''
      },
      packList: [
        {value: '0',label: '纸盒纸类'},
        {value: '1',label: '泡沫盒'},
        {value: '2',label: '冰袋'}
      ],
      modelList: [
        {value: '0',label: '1.5kg'},
        {value: '1',label: '2.5kg'}
      ]
    }
  },

  methods: {
    //类别查询
    search() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          console.log(this.form)
          this.$message.success('成功!');
        } else {
          console.log('error submit!!');
          this.$message.error('失败!');
          return false;
        }
      });
    },
    //保存表单
    save() {
      this.$refs['addForm'].validate((valid) => {
        if (valid) {
          console.log(this.addForm)
          this.addForm = {
            pack_type: '',
            model: '',
            amount: '',
            unit: '',
            price: ''
          }
          console.log(this.addForm)
          this.dialogAdd = false;
          this.$message.success('成功!');
        } else {
          console.log('error submit!!');
          this.$message.error('失败!');
          return false;
        }
      });
    },

    close() {
      this.dialogAdd = false;
      this.addForm = {
        pack_type: '',
        model: '',
        amount: '',
        unit: '',
        price: ''
      }
    },

    //编辑操作
    handleEdit(row) {
      this.dialogAdd = true;
      this.text= "修改包装材料"
      this.addForm = {
        pack_type: row.pack_type,
        model: row.model,
        amount: row.amount,
        unit: row.unit,
        price: row.price
      }
    },

    //新增
    add() {
      this.dialogAdd = true;
      this.text = "新增包装材料"
    },

    //删除操作
    deleteRow(index, rows){
      rows.splice(index, 1);
    },

    //每页条数改变时触发事件,选择一页显示多少行
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.currentPage = 1;
      this.pagesize = val;
    },
    // 当前页改变时触发 跳转其他页
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.currentPage = val;
    }
  }
}
</script>
<style lang="scss">

</style>
